package org.dromara.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.bo.BusinessConfigTypeBo;
import org.dromara.business.domain.vo.BusinessConfigTypeVo;
import org.dromara.business.service.BusinessConfigTypeService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 业务参数配置类型
 *
 * @author zj
 * @date 2024-06-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/configType")
public class BusinessConfigTypeController extends BaseController {

    private final BusinessConfigTypeService businessConfigTypeService;

    /**
     * 查询业务参数配置类型列表
     */
    @SaCheckPermission("business:configType:list")
    @GetMapping("/list")
    public TableDataInfo<BusinessConfigTypeVo> list(BusinessConfigTypeBo bo, PageQuery pageQuery) {
        return businessConfigTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出业务参数配置类型列表
     */
    @SaCheckPermission("business:configType:export")
    @Log(title = "业务参数配置类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BusinessConfigTypeBo bo, HttpServletResponse response) {
        List<BusinessConfigTypeVo> list = businessConfigTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "业务参数配置类型", BusinessConfigTypeVo.class, response);
    }

    /**
     * 获取业务参数配置类型详细信息
     *
     * @param configTypeId 主键
     */
    @SaCheckPermission("business:configType:query")
    @GetMapping("/{configTypeId}")
    public R<BusinessConfigTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long configTypeId) {
        return R.ok(businessConfigTypeService.queryById(configTypeId));
    }

    /**
     * 新增业务参数配置类型
     */
    @SaCheckPermission("business:configType:add")
    @Log(title = "业务参数配置类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BusinessConfigTypeBo bo) {
        return toAjax(businessConfigTypeService.insertByBo(bo));
    }

    /**
     * 修改业务参数配置类型
     */
    @SaCheckPermission("business:configType:edit")
    @Log(title = "业务参数配置类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BusinessConfigTypeBo bo) {
        return toAjax(businessConfigTypeService.updateByBo(bo));
    }

    /**
     * 删除业务参数配置类型
     *
     * @param configTypeIds 主键串
     */
    @SaCheckPermission("business:configType:remove")
    @Log(title = "业务参数配置类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configTypeIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] configTypeIds) {
        return toAjax(businessConfigTypeService.deleteWithValidByIds(List.of(configTypeIds), true));
    }

    /**
     * 同步超级管理员的业务参数配置到当前租户
     */
    @SaCheckPermission("business:configType:remove")
    @PutMapping("/syncConfig")
    public R<Void> syncConfig() {
        return toAjax(businessConfigTypeService.syncConfig());
    }
}
